<?php
    require_once('DAO/userDAO.class.php');
    abstract class User {
        protected $uid;
        protected $name;
        protected $surname;
        protected $login;
        protected $gid;
        protected $dateCreated;
        protected $lastLogin;
        protected $enabled;
        protected $rights;
        
        public function getUid() {
            return $this->uid;
        }
        public function getName() {
            return $this->name;
        }
        public function getSurname() {
            return $this->surname;
        }
        public function getLogin() {
            return $this->login;
        }
        public function getGid() {
            return $this->gid;
        }
        public function getDateCreated() {
            return $this->dateCreated;
        }
        public function getLastLogin() {
            return $this->lastLogin;
        }
        public function getEnabled() {
            return $this->enabled;
        }
        public function getRights() {
            return $this->rights;
        }
    }
    
    class TheUser extends User {
        private static $instance;
        
        private function __construct() {
            if($this->isLoggedIn()) {
                $this->setUserData($_SESSION['user']);
            } else {
                trigger_error("Cannot create the instance of TheUser while user is not logged in!",E_USER_ERROR);
            }
        }
        
        public static function getInstance() {
            if(!self::$instance) {
               self::$instance = new TheUser();
            }
            return self::$instance;
        }
         
        public static function isLoggedIn() {
            if(isset($_SESSION['user']['uid']) && (int)$_SESSION['user']['uid'] > 0)
                return true;
            else
                return false;
	}
        
        public static function aboutToLogIn() {
            return strstr($_SERVER['REQUEST_URI'],'login.php');
        }
        
        
	public function logOut() {
		session_destroy();
		header("Location: login.php");
	}
        
        public static function logIn($login, $password) {
            if(TheUser::isLoggedIn()) {
                return true;
            } elseif(((int)($uid = UserDAO::login($login, $password))) > 0) {
                $data = UserDAO::getUserInfo($uid);
                if(!isset($_SESSION['user'])) {
                    $_SESSION['user'] = array();
                }
                foreach($data as $key => $value) {
                    $_SESSION['user'][$key] = $value;
                }
                self::getInstance();
                return true;
            } else {
                return false;
            }
        }
        
        public function changePassword($oldPass, $newPass) {
            return UserDAO::changePassword($oldPass, $newPass);
        }
        
        private function setUserData($data) {
            foreach($data as $key => $val) {   
                if(property_exists(get_class($this),$key)) {
                    $this->$key = $val;
                }
            }
        }
        
        private function fillUserInfoFromDB() {
            if(($data = UserDAO::getUserInfo($this->uid)) !== false) {
                $this->setUserData($data);
            } else {
                return false;
            }
        }
    }
    
    class AnyUser extends User {
        
        protected $password;
        
        public function __construct($var) {
            if(get_class($var) == 'AnyUser' || get_class($var) == 'TheUser') {
                $this->copy($var);
            } elseif(is_int($var)) {
                $this->fillUserInfoFromDB($var);
            }
        }
        
        private function copy($obj) {
            $this->setUserData(get_object_vars($obj));
            $this->uid = $obj->getUid();
            $this->dateCreated = $obj->getDateCreated();
        }
        
        public function getPassword() {
            return $this->password;
        }
        
        public function setPassword($password) {
            $this->password = $password;
        }
        
        public function setName($name) {
            $this->name = $name;
        }

        public function setSurname($surname) {
            $this->surname = $surname;
        }

        public function setLogin($login) {
            $this->login = $login;
        }

        public function setGid($gid) {
            $this->gid = $gid;
        }

        public function setLastLogin($lastLogin) {
            $this->lastLogin = $lastLogin;
        }

        public function setEnabled($enabled) {
            $this->enabled = $enabled;
        }
        
        public function fillUserInfoFromDB($uid) {
            if($uid > 0) {
                if(count($data = UserDAO::getUserInfo($uid))) {
                    $this->setUserData($data);
                    $this->uid = $uid;
                    $this->dateCreated = $data['dateCreated'];
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        
        public function setUserData($data) {
            foreach($data as $key => $val) {
                if($key != 'uid' && $key != 'dateCreated') {
                    if(property_exists(get_class($this),$key)) {
                        $this->$key = $val;
                    }
                }
            }
        }
        
        public function updateUserInfoInDB() {
            return UserDAO::updateUserInfo($this);
        }
        
        public function deleteUserFromDB() {
            return UserDAO::deleteUser($this);
        }
        
        public function create() {
            return UserDAO::createUser($this);
        }
    }
?>